using UnityEngine;
using System.Collections;

public class glassOverlay : MonoBehaviour {

	public static float alphaLevel = 0.50f;
	SpriteRenderer alphaRenderer;
	
	void Start(){
		alphaRenderer = gameObject.GetComponent<SpriteRenderer>();
	}


	// Update is called once per frame
	void Update () {

		ResizeSpriteToScreen ();

		alphaLevel = 0.55f - (GameManager.playerHP / 100f);

		if (GameManager.playerHP < GameManager.lowHPth) 
			alphaRenderer.color = new Color (0f, 0f, 0f, alphaLevel);
		if (GameManager.playerHP <= 0) 
			alphaRenderer.color = new Color (0f, 0f, 0f, 1f);
		else
			alphaRenderer.color = new Color (0f, 0f, 0f, 0f);
	}


	void ResizeSpriteToScreen()
	{
		SpriteRenderer sr=GetComponent<SpriteRenderer>();
		if(sr==null) return;
		
		transform.localScale=new Vector3(1,1,1);
		
		float width=sr.sprite.bounds.size.x;
		float height=sr.sprite.bounds.size.y;
		
		
		float worldScreenHeight=Camera.main.orthographicSize*2f;
		float worldScreenWidth=worldScreenHeight/Screen.height*Screen.width;
		
		Vector3 xWidth = transform.localScale;
		xWidth.x=worldScreenWidth / width;
		transform.localScale=xWidth;
		//transform.localScale.x = worldScreenWidth / width;
		Vector3 yHeight = transform.localScale;
		yHeight.y=worldScreenHeight / height;
		transform.localScale=yHeight;
		//transform.localScale.y = worldScreenHeight / height;
		
	}
}
